package com.summer.socket;

import java.net.InetSocketAddress;
import java.util.Scanner;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class SimpleCase5 {
	
	public static void main(String[] args) {
		
		// 服务启动引导程序类
		Bootstrap bootstrap = new Bootstrap();
		
		// 线程池
		EventLoopGroup group = new NioEventLoopGroup();
		
		// 设置工厂
		bootstrap.group(group)
			.channel(NioSocketChannel.class)
			.remoteAddress(new InetSocketAddress(9999))
			.handler(new ChannelInitializer<Channel>(){
				@Override
				protected void initChannel(Channel ch) throws Exception {
					ChannelPipeline pipeline = ch.pipeline();
					pipeline.addLast("decoder", new StringDecoder());
					pipeline.addLast("encoder", new StringEncoder());
					pipeline.addLast("hiHandler",new HiHandler());
				}
			});
		// 连接服务器端
		ChannelFuture connect = bootstrap.connect();
		Channel channel = connect.channel();

		Scanner scanner = new Scanner(System.in);
		while(true){
			System.out.println("请输入");
			channel.writeAndFlush(scanner.next());
		}
	}
	
	public static class HiHandler extends SimpleChannelInboundHandler<String>{

		/**
		 * 向EventLoop中注册了通道
		 * 可在此做IP过滤黑名单
		 */
		@Override
		public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
			System.out.println("channelRegistered");
			super.channelRegistered(ctx);
		}
		
		/**
		 * 通道被激活了，处于激活状态
		 */
		@Override
		public void channelActive(ChannelHandlerContext ctx) throws Exception {
			System.out.println("channelActive");
			super.channelActive(ctx);
		}
		
		/**
		 * 读取通道数据
		 */
		@Override
		public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
			System.out.println("channelRead::"+msg);
			super.channelRead(ctx, msg);
			
//			ctx.channel().writeAndFlush(msg);
//			try {
//				int a = 1/0;
//			} catch (Exception e) {
//				// TODO: handle exception
//			}
		}
		
		@Override
		protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
			System.out.println("channelRead0::"+msg);
			
		}

		/**
		 * 捕获异常
		 */
		@Override
		public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
			System.out.println("exceptionCaught");
			super.exceptionCaught(ctx, cause);
		}
		
		/**
		 * 读取后出发完成
		 */
		@Override
		public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
			System.out.println("channelReadComplete");
			super.channelReadComplete(ctx);
		}
		
		/**
		 * 通道已注册，现在停用并且达到生命结束
		 */
		@Override
		public void channelInactive(ChannelHandlerContext ctx) throws Exception {
			System.out.println("channelInactive");
			super.channelInactive(ctx);
		}
		
		/**
		 * 从EventLoop中取消通道注册
		 */
		@Override
		public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
			System.out.println("channelUnregistered");
			super.channelUnregistered(ctx);
		}
		
	}

}
